import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/page/base_page_state.dart';
import 'package:flutter_demo/page/news/news_detail_page.dart';
import 'package:flutter_demo/util/ui_util.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends BasePageState<HomePage> {
  ScrollController _scrollController = ScrollController();
  RefreshController _refreshController = RefreshController(initialRefresh: false);
  double _opacity = 0.0;
  double _bannerHeight = 250.0;
  @override
  void initState() {
    super.initState();
    appBarController.extendBodyBehindAppBar = true;
    appBarController.title = '首页';
    addAppBarImageAction('res/images/ic_share.png', () {
      _showShareActionSheet();
    });
    _scrollController.addListener(() {
      if (_scrollController.offset < 1) {
        if (_opacity > 0.0) {
          setState(() {
            _opacity = 0.0;
          });
        }
      } else if (_scrollController.offset > _bannerHeight - APP_BAR_HEIGHT) {
        if (_opacity < 1) {
          setState(() {
            _opacity = 1.0;
          });
        }
      } else {
        setState(() {
          _opacity = _scrollController.offset / (_bannerHeight - APP_BAR_HEIGHT);
        });
      }
    });
  }

  @override
  Widget buildBody(BuildContext context) {
    return SmartRefresher(
      controller: _refreshController,
      onRefresh: () => _refreshController.refreshCompleted(),
      child: CustomScrollView(
        controller: _scrollController,
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: Opacity(
                opacity: 1 - _opacity,
                child: Container(
                  height: _bannerHeight,
                  child: PageView.builder(
                    itemBuilder: (context, index) =>
                        CachedNetworkImage(
                          imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584804992842&di=61f2dce61d16002bd2edf133af74c4e8&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2F9a673c53683e7ae9031398ab437c8d65b53ee431.jpg',
                          fit: BoxFit.cover,
                        ),
                    itemCount: 4,
                  ),
                )
            ),
          ),
          SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4, childAspectRatio: 1.0),
              delegate: SliverChildBuilderDelegate(_buildIcon, childCount: 8,)
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(_buildItem, childCount: 20),
          )
        ],
      ),
    );
  }

  Widget _buildIcon(BuildContext context, int index) {
    return InkWell(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset('res/images/ic_wx.png', width: 48, height: 48),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text('主题$index'),
          )
        ],
      ),
      onTap: () {
        Fluttertoast.showToast(msg: '主题$index');
      },
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    return Column(
      children: <Widget>[
        InkWell(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
            child: Row(
              children: <Widget>[
                CachedNetworkImage(
                  width: 80,
                  height: 80,
                  imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531798262708&di=53d278a8427f482c5b836fa0e057f4ea&imgtype=0',
                  fit: BoxFit.cover,
                ),
                Expanded(
                    child: Padding(
                      padding: EdgeInsets.only(left: 10),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text('这是一个主标题，关于橘子',
                              style: TextStyle(fontSize: 16)
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 5),
                            child: Text('这是一个副标题，🍊橘子是秋季成长的水果，可以去热，补充维生素C，增强身体抵抗力',
                                style: TextStyle(
                                    fontSize: 14, color: Colors.black54)
                            ),
                          ),
                        ],
                      ),
                    )
                )
              ],
            ),
          ),
          onTap: _handleClick,
        ),
        Container(
          height: 0.5,
          color: Colors.black12,
        )
      ],
    );
  }

  _handleClick() {
    pushPage(NewsDetailPage());
  }

  _showShareActionSheet() {
    UIUtil.share(this.context);
  }

  @override
  Widget buildAppBar(BuildContext context) {
    return Opacity(
      opacity: _opacity,
      child: super.buildAppBar(context),
    );
  }
}